<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
const animationDuration = 3000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    monthPostData: {
      type: String,
      default: new Array()
    },
    monthReplyData: {
      type: String,
      default: new Array()
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '330px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      //生成单纯日期
      let DATE=[];
      for(let i=0;i<this.monthPostData.length;i++){
        DATE.push(this.monthPostData[i].HOUR)
      }
      //提取数值
      let MONTHDATE=[];
      for(let i=0;i<this.monthPostData.length;i++){
        MONTHDATE.push(this.monthPostData[i].COUNT)
      }
      let MONTHREPLYDATE=[];
      for(let i=0;i<this.monthReplyData.length;i++){
        MONTHREPLYDATE.push(this.monthReplyData[i].COUNT)
      }

      const colors = ['#5470C6', '#EE6666'];
      let option = {
        color: colors,

        tooltip: {
          trigger: 'none',
          axisPointer: {
            type: 'cross'
          }
        },
        legend: {},
        grid: {
          top: 70,
          bottom: 50
        },
        xAxis: [
          {
            type: 'category',
            axisTick: {
              alignWithLabel: true
            },
            axisLine: {
              onZero: false,
              lineStyle: {
                color: colors[1]
              }
            },
            axisPointer: {
              label: {
                formatter: function (params) {
                  return (
                      '回复  ' +
                      params.value +
                      (params.seriesData.length ? '：' + params.seriesData[0].data : '')
                  );
                }
              }
            },
            // prettier-ignore
            data:DATE,
          },
          {
            type: 'category',
            axisTick: {
              alignWithLabel: true
            },
            axisLine: {
              onZero: false,
              lineStyle: {
                color: colors[0]
              }
            },
            axisPointer: {
              label: {
                formatter: function (params) {
                  return (
                      '发布  ' +
                      params.value +
                      (params.seriesData.length ? '：' + params.seriesData[0].data : '')
                  );
                }
              }
            },
            // prettier-ignore
            data: DATE,
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: '发布留言',
            type: 'line',
            xAxisIndex: 1,
            smooth: true,
            emphasis: {
              focus: 'series'
            },
            data:MONTHDATE
          },
          {
            name: '回复留言',
            type: 'line',
            smooth: true,
            emphasis: {
              focus: 'series'
            },
            data: MONTHREPLYDATE
          }
        ]
      };
      this.chart.setOption(option)
    }
  }
}
</script>
